"use strict";

const { Controller } = require("egg");

class LiveController extends Controller {
  // 获取资讯数据
  async getLive() {
    const { ctx } = this;
    const { type, value, page } = ctx.query;
    const list = await this.app.mysql.select("live");
    ctx.body = {
      code: 200,
      total: list.length,
      list: list
        .filter((v) => {
          if (type === "全部") {
            return true;
          }
          return v.state === type;
        })
        .filter((v) => {
          if (value == "") {
            return true;
          }
          return v.name.includes(value) || v.title.includes(value);
        })
        .slice(0, page),
    };
  }

  async disableChange() {
    const { ctx } = this;
    const { type, key } = ctx.request.body;
    if (type == "开启") {
      const res = await this.app.mysql.update(
        "live",
        { type: "关闭" },
        { where: { key } }
      );
      if (res.affectedRows === 1) {
        ctx.body = {
          code: 200,
        };
      } else {
        ctx.body = {
          code: 300,
        };
      }
    } else {
      const res = await this.app.mysql.update(
        "live",
        { type: "开启" },
        { where: { key } }
      );
      if (res.affectedRows === 1) {
        ctx.body = {
          code: 200,
        };
      } else {
        ctx.body = {
          code: 300,
        };
      }
    }
  }
}

module.exports = LiveController;
